package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CustomerProblemReply;
import com.isunland.managesystem.utils.MyDateUtil;

/**
 * Created by Lenovo on 2017/3/1.
 */
public class CustomerProblemReplyFragment extends Fragment {

  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.CustomerProblemReplyFragment.EXTRA_CONTENT";
  @BindView(R.id.tv_replayContent) TextView tvReplayContent;
  @BindView(R.id.tv_remark) TextView tvRemark;
  @BindView(R.id.tv_regStaffName) TextView tvRegStaffName;
  @BindView(R.id.tv_regDate) TextView tvRegDate;
  @BindView(R.id.tv_file) TextView tvFile;
  private CustomerProblemReply mContent;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    ActionBar actionBar = ((BaseVolleyActivity) getActivity()).getSupportActionBar();
    if (actionBar != null) {
      actionBar.setTitle(R.string.reply_detail);
      actionBar.setDisplayHomeAsUpEnabled(true);
    }
    mContent = (CustomerProblemReply) getArguments().getSerializable(EXTRA_CONTENT);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_problrm_reply, container, false);
    ButterKnife.bind(this, view);
    bindData();
    return view;
  }

  private void bindData() {
    tvReplayContent.setText(mContent.getReplayContent());
    tvRemark.setText(mContent.getRemark());
    tvRegStaffName.setText(mContent.getRegStaffName());
    tvRegDate.setText(MyDateUtil.parseDateToString(MyDateUtil.stringToDate(mContent.getRegDate())));
    tvFile.setText(mContent.getFileOriginalName());
  }

  @OnClick(R.id.tv_file) public void onClick() {
    downFile(mContent.getFilepath1());
  }

  private void downFile(String serverPath) {
    if (!TextUtils.isEmpty(serverPath)) {
      ExtraDownLoadDialogFragment dialog = ExtraDownLoadDialogFragment.newInstance("", serverPath);
      FragmentManager fm = getActivity().getSupportFragmentManager();
      dialog.show(fm, "");
    }
  }

  public static CustomerProblemReplyFragment newInstance(CustomerProblemReply content) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, content);
    CustomerProblemReplyFragment fragment = new CustomerProblemReplyFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
